import { pool } from '../db'
import { processHtml } from '../process-html'

export interface BlogPost {
  post_date: string
  post_title: string
  post_content: string
  [key: string]: unknown
}

/**
 * 从数据库获取 WordPress 文章详情
 */
export async function fetchWpPostById(id: string): Promise<BlogPost | null> {
  const client = await pool.getConnection()

  try {
    const query = `
      SELECT *
      FROM wp_posts
      WHERE ID = ?
    `

    const [rows] = await client.query(query, [id])

    if (Array.isArray(rows) && rows.length > 0) {
      const post = rows[0] as BlogPost

      if (post.post_content) {
        post.post_content = await processHtml(String(post.post_content))
      }

      return post
    }

    return null
  } finally {
    client.release()
  }
}